package com.fk.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.fk.entity.XinWenXiangQing;
import com.fk.uitl.DBManager;

public class XinWenXiangQingDaoImpl {
	// 鏂伴椈璇︽儏
	public List<XinWenXiangQing> getXinWenXiangQingList(int cId) {
		List<XinWenXiangQing> xwxqList = new ArrayList<XinWenXiangQing>();

		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from xinwenxiangqing where xinWenFenLeiId=" + cId);
			while (rs.next()) {
				int xinWenNeiRongId = rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi = rs.getString("xinWenBiaoTi");
				String xinWenNeiRong = rs.getString("xinWenNeiRong");
				int xinWenFenLeiId = rs.getInt("xinWenFenLeiId");
				XinWenXiangQing xwxq = new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong,
						xinWenFenLeiId);
				xwxqList.add(xwxq);
			}
			return xwxqList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}

	// 鏂伴椈璇︽儏鍜ㄨ
	public List<XinWenXiangQing> getxwxqList(int gId) {
		List<XinWenXiangQing> NewsInforMationList = new ArrayList<XinWenXiangQing>();

		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from xinwenxiangqing where xinWenNeiRongId=" + gId);
			while (rs.next()) {
				int xinWenNeiRongId = rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi = rs.getString("xinWenBiaoTi");
				String xinWenNeiRong = rs.getString("xinWenNeiRong");
				XinWenXiangQing xwxq = new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong);
				NewsInforMationList.add(xwxq);
			}
			return NewsInforMationList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}

	// 棣栭〉鏂伴椈璧勮
	public List<XinWenXiangQing> getsyxwList() {
		List<XinWenXiangQing> syxwList = new ArrayList<XinWenXiangQing>();

		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from xinwenxiangqing order by rand()");
			while (rs.next()) {
				int xinWenNeiRongId = rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi = rs.getString("xinWenBiaoTi");
				String xinWenNeiRong = rs.getString("xinWenNeiRong");
				XinWenXiangQing xwxq = new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong);
				syxwList.add(xwxq);
			}
			return syxwList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}

	// 锟斤拷一锟斤拷锟斤拷锟斤拷
	public List<XinWenXiangQing> getShangYiTiaoList(int sId) {
		List<XinWenXiangQing> ShangYiTiaoList = new ArrayList<XinWenXiangQing>();

		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			rs = st.executeQuery(
					"select * from xinwenxiangqing where xinWenNeiRongId in (select max(xinWenNeiRongId) from xinwenxiangqing where xinWenNeiRongId< "
							+ sId + ") ");
			while (rs.next()) {
				int xinWenNeiRongId = rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi = rs.getString("xinWenBiaoTi");
				String xinWenNeiRong = rs.getString("xinWenNeiRong");
				XinWenXiangQing ShangYiTiao = new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong);
				ShangYiTiaoList.add(ShangYiTiao);
			}
			return ShangYiTiaoList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}

	// 锟斤拷一锟斤拷锟斤拷锟斤拷
	public List<XinWenXiangQing> getXiaYiTiaoList(int xId) {
		List<XinWenXiangQing> XiaYiTiaoList = new ArrayList<XinWenXiangQing>();

		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			rs = st.executeQuery(
					"select * from xinwenxiangqing where xinWenNeiRongId in (select min(xinWenNeiRongId) from xinwenxiangqing where xinWenNeiRongId> "
							+ xId + ") ");
			while (rs.next()) {
				int xinWenNeiRongId = rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi = rs.getString("xinWenBiaoTi");
				String xinWenNeiRong = rs.getString("xinWenNeiRong");
				XinWenXiangQing XiaYiTiao = new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong);
				XiaYiTiaoList.add(XiaYiTiao);
			}
			return XiaYiTiaoList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}

	public List<XinWenXiangQing> getXinWenXiangQing() {
		List<XinWenXiangQing> xwxqList = new ArrayList<XinWenXiangQing>();

		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from xinwenxiangqing");
			while (rs.next()) {
				int xinWenNeiRongId = rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi = rs.getString("xinWenBiaoTi");
				String xinWenNeiRong = rs.getString("xinWenNeiRong");
				int xinWenFenLeiId = rs.getInt("xinWenFenLeiId");
				XinWenXiangQing xwxq = new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong,
						xinWenFenLeiId);
				xwxqList.add(xwxq);
			}
			return xwxqList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 根据新闻详情内容id删除新闻详情
	 * @param xId 新闻详情内容id
	 * @return 返回数据库受影响的行数
	 */
	public int delectXinWenNeiRongId( int xId) {
		return DBManager.executeUpdate("delete from xinwenxiangqing where xinWenNeiRongId="+xId);
	}
	/**
	 * 多条件查询新闻详情
	 * @param q 
	 * @return
	 */
	public List<XinWenXiangQing> getNewsDetail(XinWenXiangQing q){
		List<XinWenXiangQing> ndList=new ArrayList<XinWenXiangQing>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			String sql="select xq.*,fl.xinWenFenLei from xinwenxiangqing xq,xinwenfenlei fl where 1=1";
			if(q.getXinWenNeiRongId()!=0) {//�ǿ��ж�
				sql+=" and xinWenNeiRongId like '%"+q.getXinWenNeiRongId()+"%'";
			}
			if(q.getXinWenBiaoTi()!=null && !q.getXinWenBiaoTi().equals("")) {//�ǿ��ж�
				sql+=" and xinWenBiaoTi like '%"+q.getXinWenBiaoTi()+"%'";
			}
			if(q.getXinWenNeiRong()!=null && !q.getXinWenNeiRong().equals("")) {//�ǿ��ж�
				sql+=" and xinWenNeiRong like '%"+q.getXinWenNeiRong()+"%'";
			}
			if(q.getXinWenFenLeiId()!=0) {//�ǿ��ж�
				sql+=" and xinWenFenLeiId="+q.getXinWenFenLeiId();
			}
			sql+=" and xq.xinWenFenLeiId=fl.xinWenId";
			
			System.out.println(sql);//�鿴sql����Ƿ�ƴ��
			
			rs=st.executeQuery(sql);
			while(rs.next()) {
				int xinWenNeiRongId=rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi=rs.getString("xinWenBiaoTi");
				String xinWenNeiRong=rs.getString("xinWenNeiRong");
				int xinWenFenLeiId=rs.getInt("xinWenFenLeiId");
				String xinWenFenLei=rs.getString("xinWenFenLei");
				
				XinWenXiangQing nd=new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong, xinWenFenLeiId, xinWenFenLei);
				ndList.add(nd);
			}
			return ndList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public int addXinWenNeiRongId(XinWenXiangQing xq) {
		String sql = "INSERT INTO xinwenxiangqing VALUES(NULL,'"+xq.getXinWenBiaoTi()+"','"+xq.getXinWenNeiRong()+"',"+xq.getXinWenFenLeiId()+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	public	List<XinWenXiangQing> getXinWenXiangQingName(String name){
		List<XinWenXiangQing> xinList = new ArrayList<XinWenXiangQing>();
		XinWenXiangQing x = null;
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from xinwenxiangqing where xinWenBiaoTi = '"+name+"'");
			while(rs.next()) {
				int xinWenNeiRongId=rs.getInt("xinWenNeiRongId");
				String xinWenBiaoTi=rs.getString("xinWenBiaoTi");
				String xinWenNeiRong=rs.getString("xinWenNeiRong");
				int xinWenFenLeiId=rs.getInt("xinWenFenLeiId");
				
				x = new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong, xinWenFenLeiId);
				xinList.add(x);
			}
			System.out.println(x);
			return xinList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	//查询修改
	public XinWenXiangQing getXinWenXiangQing(int id) {
		Connection conn=DBManager.getConnection();
		XinWenXiangQing Xq=null;
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st=conn.createStatement();
			rs=st.executeQuery("\r\n" + "SELECT*FROM xinwenxiangqing WHERE xinWenNeiRongId="+id);
			while(rs.next()) {
				int nrId=rs.getInt(1);
				String bT=rs.getString(2);
				String  nR=rs.getString(3);
				int fL=rs.getInt(4);
				Xq=new XinWenXiangQing(nrId, bT, nR, fL);
			}
			return Xq;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	//查询修改
public int upadateXiangQing(XinWenXiangQing xq) {
		
		return DBManager.executeUpdate("UPDATE xinwenxiangqing SET xinWenBiaoTi='"+xq.getXinWenBiaoTi()+"',xinWenNeiRong='"+xq.getXinWenNeiRong()+"',xinWenFenLeiId="+xq.getXinWenFenLeiId()+" WHERE xinWenNeiRongId="+xq.getXinWenNeiRongId());
		
	}
}



